import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '@/views/Login/Login'
import Reg from '@/views/Reg/Reg'
import Main from '@/views/Main/Main'
import Home from '@/views/Muens/Home/Home'
import UserInfo from '@/views/Muens/User/UserInfo'
import UserAvatar from '@/views/Muens/User/UserAvatar'
import UserPwd from '@/views/Muens/User/UserPwd'
import ArtCate from '@/views/Muens/Article/ArtCate'
import ArtList from '@/views/Muens/Article/ArtList'
// 导入store
import store from '@/store'

Vue.use(VueRouter)

const routes = [
  { path: '/login', component: Login },
  { path: '/reg', component: Reg },
  {
    path: '/',
    component: Main,
    redirect: '/home',
    children: [
      { path: 'user-info', component: UserInfo },
      { path: 'user-avatar', component: UserAvatar },
      { path: 'user-pwd', component: UserPwd },
      { path: 'art-cate', component: ArtCate },
      { path: 'art-list', component: ArtList },
      { path: 'home', component: Home }
    ]
  }

]

const router = new VueRouter({
  routes
})
router.beforeEach((to, from, next) => {
  const token = store.state.token
  if (!token && to.path !== '/login' && to.path !== '/reg') {
    Vue.prototype.$message.error('您还没登录')
    next('/login')
  } else {
    next()
  }
})

export default router
